require(['config'],function(){
    require(['jquery','cookie','loadHF','fly'],function($){
        const Info = JSON.parse($.cookie('goodsInfo'));
        const memoryLis = $('.option-memory-box>ul li');
        const colorLis = $('.option-color-box>ul li');
        $('.pInfo>span.title').html(Info.title);
        $('.pInfo>a').html(Info.title+'pro');
        $('h2').html(Info.title);
        $('.show-img>img').attr('src', Info.image);
        $('.price-info').html(Info.curPrice);
        $('font').html(Info.des);
        $('.shopping-info>span.price').html(Info.curPrice);
        $('.shopping-info>span.config').html(`${Info.title} 6GB+128GB`);
        $('.shopping-info>span.color').html('幻青');
        $('.total-price').html("总价"+Info.curPrice);
        //配置选择
        $('.option-memory-box ul.clearfix').on('click',function(e){
            if($(e.target).hasClass("clearfix")){
                return
            }
            for(let i = 0;i<memoryLis.length;i++){
                $(memoryLis[i]).css({"borderColor":"#e0e0e0","color":"black"})
            }
            $(e.target).css({"borderColor":"#ff6700","color":"#ff6700"});
            const length = Info.curPrice.length;
            if($(e.target).html()=="8GB+128GB"){
                $('.price-info').html(String(Number(Info.curPrice.slice(0,length-1))+200)+"元");
                $('.shopping-info>span').html(String(Number(Info.curPrice.slice(0,length-1))+200)+"元");
                $(".total-price").html("总价"+String(Number(Info.curPrice.slice(0,length-1))+200)+"元")
                $('.shopping-info>span.config').html(`${Info.title} ${$(e.target).html()}`);
            }
            else if($(e.target).html()=="8GB+256GB"){
                $('.price-info').html(String(Number(Info.curPrice.slice(0,length-1))+500)+"元");
                $('.shopping-info>span').html(String(Number(Info.curPrice.slice(0,length-1))+500)+"元");
                $(".total-price").html("总价"+String(Number(Info.curPrice.slice(0,length-1))+500)+"元")
                $('.shopping-info>span.config').html(`${Info.title} ${$(e.target).html()}`);
            }
            else{
                $('.price-info').html(Info.curPrice);
                $('.shopping-info>span').html(Info.curPrice);
                $('.total-price').html("总价"+Info.curPrice);
                $('.shopping-info>span.config').html(`${Info.title} ${$(e.target).html()}`);
            }
        })
        //颜色选择
        $('.option-color-box ul.clearfix').on('click',function(e){
            if($(e.target).hasClass("clearfix")){
                return
            }
            for(let i = 0;i<colorLis.length;i++){
                $(colorLis[i]).css({"borderColor":"#e0e0e0","color":"black"})
            }
            $(e.target).css({"borderColor":"#ff6700","color":"#ff6700"});
            $('.shopping-info>span.color').html(`${$(e.target).html()}`);
            
        })

        $('.sale-btn').on('click',function(e){
            //加入购物车
            //配置cookie时自动实现JSON转换
            $.cookie.json = true;
            const cart = $.cookie('cart') || [];
            const curInfo = $.cookie("goodsInfo");
            //更新补充curInfo
            curInfo.curPrice = $('.shopping-info>span.price').html();
            curInfo.config = $('.shopping-info>span.config').html();
            curInfo.color = $('.shopping-info>span.color').html();
            $.cookie('goodsInfo',curInfo,{expires:7,path:"/"});
            const index = cart.findIndex(item=>item.id===curInfo.id);
            if(index===-1){
                cart.push(curInfo);
            }
            else{
                cart[index].amount++;
            }
            $.cookie('cart',cart,{expires:7});
            
            //添加抛物线效果
            const {top,left} = $('.aside>li:nth-child(5)').offset();
            let scrollTop = $(document).scrollTop();
            $(`<img src =${Info.image} style="width:32px;height:32px;position:relative;z-index:99"/>`).fly({
                start:{
                    top:e.clientY,
                    left:e.clientX
                },
                end:{
                    top:top-scrollTop,
                    left
                },
                speed:0.8,
                onEnd(){
                    this.destroy();
                    location.href="http://localhost:8888/html/cart.html"
                }
            })
        })

    })
})
